// pages/userDetail/index.js
const app = getApp();
const db = wx.cloud.database();
import {
	getPraiseNum,
	getUploadNum,
	goBack
} from "../../Utils/Utils"


Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		CustomBar: app.globalData.CustomBar,
		StatusBar: app.globalData.StatusBar, // 状态栏高度
		windowHeight: app.globalData.windowHeight, // 屏幕整体高度
		scrollHeight: 0, // scroll高度
		showHistory: true, // 是否愿意展示以往发帖||或者没发过贴
		name: "", // 用户名字
		gender: 0, // 性别 枚举0/1
		college: "", // 学院
		sign: "", // 个性签名
		img: "", // 头像
		user_openid: "", // 该用户的openid
		limit: 5, // 每页多少数据
		HistoryList: [], // 每个对象就是一个帖子
		PicturesShow: false, 				// 是否显示图片展示
		pictures: [], 						// 这个是全屏展示图片, 不能用本地图片
		current:0,							// 画廊下标
	},
	/**
	 * 加载数据
	 */
	loadArticles() {
		let that = this;
		that.openBombBox("加载中...")
		let skip = that.data.HistoryList.length;
		let limit = that.data.limit
		if (skip % limit == 0) {
			// 当skip不为 limit 整除 就说明已经是最后一页了
			//可以尝试获取下一页数据
			wx.cloud.callFunction({
				name: "operation",
				data: {
					function: "getActicleList",
					skip: skip,
					limit: limit,
					openid: that.data.user_openid
				} 
			}).then(res => {
				let acticlelists = res.result.data;
				// 本地data文章列表
				let datalist = []
				for (let al of acticlelists) {
					datalist.push({
						flagId: al._id,
						type: al.type
					})
				}
				that.setData({
					HistoryList: that.data.HistoryList.concat(datalist)
				})
				that.closeBombBox()
			}).catch(err => {
				console.log(err)
				that.closeBombBox()
			})
		} else {
			that.closeBombBox()
			that.optionBombBox2Show("没有了^_@_^")
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let that = this
		that.setData({
			scrollHeight: that.data.windowHeight - that.data.CustomBar,
		})
		that.getCurrentUserInfo(options.openid)
	},

	// 获取当前用户信息
	getCurrentUserInfo: function (openid) {
		let that = this;
		// 讲id赋值到this.user_openid
		that.setData({
			user_openid: openid,
		})
		// 获取个人信息
		db.collection("users").where({
			_openid: openid
		}).get().then(res => {
			let user = res.data[0];
			that.setData({
				showHistory: user.showHistory, // 是否愿意展示以往发帖||或者没发过贴
				name: user.nickName, // 用户名字
				gender: user.gender, // 性别 枚举0/1
				college: user.college, // 学院
				sign: user.sign, // 个性签名
				img: user.avatarUrl, // 头像
			})
		}).catch(err => {
			console.log(err)
		})
		// 获取获赞数量
		getPraiseNum(openid, res => {
			that.setData({
				like: res
			})
		})
		// 获取发布总数量
		getUploadNum(openid, res => {
			that.setData({
				issue: res
			})
		})
		// 获取此人所有的发布的文章
		that.loadArticles()
	},

	goBack,

	goDetailBeUnusedToSell(param) { // 跳转至闲置出让详情
		let {
			data
		} = param.detail
		wx.navigateTo({
			url: '../detailBeUnusedToSell/index',
			success: function (res) {
				res.eventChannel.emit("getData", {
					data: data
				})
			}
		})
	},

	goDetailLoseAndReceive(param) { // 跳转至失物招领出让详情
		let {
			data
		} = param.detail
		wx.navigateTo({
			url: '../detailLoseAndReceive/index',
			success: function (res) {
				res.eventChannel.emit("getData", {
					data: data
				})
			}
		})
	},

	goDetailExpress(param) { // 跳转至我要表白详情
		let {
			data
		} = param.detail
		wx.navigateTo({
			url: '../detailExpress/index',
			success: function (res) {
				res.eventChannel.emit("getData", {
					data: data
				})
			}
		})
	},

	optionBombBox2Show: function (param) {
		let that = this
		let temp = that.data.BombBox2Show
		let temp2 = that.data.BombBoxShow
		if (temp || temp2) {
			return
		}
		that.setData({
			BombBox2Show: true,
			BombBox2text: param
		})
		setTimeout(() => {
			that.setData({
				BombBox2Show: false,
			})
		}, 3000)
	},

	openBombBox: function (param) {
		let that = this
		that.setData({
			BombBoxShow: true,
			BombBoxText: param
		})
	},

	closeBombBox: function () {
		let that = this
		that.setData({
			BombBoxShow: false,
		})
	},
	
	
	changePic(e) { // 显示隐藏预览图片组件
		let {
			pictures,
			index
		} = e.detail
		this.setData({
			current: index,
			pictures: pictures,
			PicturesShow: true
		})
	},
	
	change(e) { // 切换图片触发
	},
	
	hide: function () { // 关闭画廊触发
		this.setData({
			pictures: []
		})
	},
	
	
	/**
	 * 触底加载数据
	 */
	bindscrolltolower() {
		this.loadArticles()
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})